#include "VDSmile.h"

#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <QDir>

VDSmiles::VDSmiles()
{
	m_folderPath = QObject::tr("data/smiles");
//	m_folderPath = QObject::tr("msgs/smiles");
}

VDSmiles::~VDSmiles()
{
	clear();
}

void VDSmiles::clear()
{
	foreach(VDSmile* s, m_smiles)
	{
		delete s;
		s = NULL;
	}

	m_smiles.clear();
}

bool VDSmiles::isEmpty() const
{
	return m_smiles.isEmpty();
}

QString VDSmiles::getSmilePath( const VDSmile* s ) const
{
	if(!s->file().isEmpty())
		return m_folderPath + '/' + s->file();

	return QString();
}

void VDSmiles::addSmile( VDSmile* s )
{
	m_smiles.push_back(s);
}

VDSmile* VDSmiles::getSmile( int ind ) const
{
	if (ind < 0 || ind >= m_smiles.size())
		return NULL;

	return m_smiles[ind];
}

bool VDSmiles::delSmile( int ind )
{
	if (ind < 0 || ind >= m_smiles.size())
		return false;

	//m_smiles[ind];
	m_smiles.remove(ind);
	return true;
}

QString VDSmiles::insertSmileToStr( const QString& str ) const
{
	QString tmp(str);

	QDir dir;
	foreach(const VDSmile* s, m_smiles)
	{
		QString repl(QObject::tr("<img src='%1' alt='[%2]'>")
			//			.arg(dir.absoluteFilePath(m_folderPath + '/' + s->m_file))
			.arg(m_folderPath + '/' + s->file())
			.arg(s->comment()));
		//vdLogDef() << repl;
		tmp.replace(s->code(), repl);
	}

	return tmp;
}

bool VDSmiles::saveToXml() const
{
	const int Indent = 4;
	QDomDocument doc;
	QDomElement root = doc.createElement(QObject::tr("smiles"));
	doc.appendChild(root);

	foreach(const VDSmile* s, m_smiles)
	{
		QDomElement smileElem = makeSmileElement(doc, s);
		root.appendChild(smileElem);
	}

	QFile file(m_folderPath + QObject::tr("/rules.xml"));
	if (file.open(QIODevice::WriteOnly))
	{
		QTextStream out(&file);
		out.setCodec(QTextCodec::codecForName("UTF-8"));
		doc.save(out, Indent, QDomNode::EncodingFromTextStream);
		file.close();
		return true;
	}

	return false;
}

bool VDSmiles::loadFromXml()
{
	bool isOk = false;
	QFile file(m_folderPath + QObject::tr("/rules.xml"));
	QDomDocument doc;

	if (file.open(QIODevice::ReadOnly))
	{
		if (doc.setContent(&file))
		{
			QDomElement root = doc.documentElement();
			if(root.tagName() == QObject::tr("smiles"))
			{
				clear();

				QDomNode node = root.firstChild();
				while (!node.isNull())
				{
					if (node.toElement().tagName() == QObject::tr("smile"))
					{
						VDSmile *s = new VDSmile("error", "error", "error");
						readSmileProperty(node.toElement(), s);
						m_smiles.push_back(s);
					}
					node = node.nextSibling();
				}


			}

			//				readNode(domElement);
		}

		file.close();
	}

	return isOk;
}

QDomElement VDSmiles::makeSmileElement( QDomDocument &doc, const VDSmile *s ) const
{
	QDomElement domElement = doc.createElement(QObject::tr("smile"));
	domElement.appendChild(makeSmileProperty(doc, QObject::tr("code"), s->code()));
	domElement.appendChild(makeSmileProperty(doc, QObject::tr("file"), s->file()));
	domElement.appendChild(makeSmileProperty(doc, QObject::tr("comment"), s->comment()));
	return domElement;
}

void VDSmiles::readSmileProperty( const QDomElement &elem, VDSmile *s )
{
	QDomNode node = elem.firstChild();
	while (!node.isNull())
	{
		if (node.toElement().tagName() == QObject::tr("code"))
			s->setCode(node.toElement().text());
		if (node.toElement().tagName() == QObject::tr("file"))
			s->setFile(node.toElement().text());
		if (node.toElement().tagName() == QObject::tr("comment"))
			s->setComment(node.toElement().text());

		node = node.nextSibling();
	}
}

QDomElement VDSmiles::makeSmileProperty( QDomDocument &doc, const QString &name, const QString &value ) const
{
	QDomElement domElement = doc.createElement(name);
	QDomText domText = doc.createTextNode(value);
	domElement.appendChild(domText);
	return domElement;
}
